import axios, { AxiosRequestConfig } from "axios";
import { message } from "ant-design-vue";
import user from "../store/modules/route";
const Axios = axios.create({
  baseURL: "http://localhost:9999",
});

Axios.interceptors.request.use(
  (conflg: AxiosRequestConfig) => {
    const token = user.state.token;
    const flag: any = conflg || {};
    if (token) {
      flag.headers["Authorization"] = "Bearer " + token;
    }
    return conflg;
  },
  (error) => {
    return Promise.reject(error);
  }
);

Axios.interceptors.response.use(
  (response) => {
    if (response.status == 200 || response.status == 201) {
      return Promise.resolve(response);
    } else {
      return Promise.reject(response);
    }
  },
  (error) => {
    if (error.response.status == 401) {
      message.error(String(error.response.data.message));
    }
    return Promise.reject(error.response);
  }
);

export default Axios;
